from rest_framework import serializers

from backend.django_res.orm.models import (SupplierModel, BuyOrderModel, TakeDeliveryModel, RecvInvoiceModel,
                                           Payment4BuyOrderModel)


class SupplierSerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierModel
        fields = ('id', 'name', 'contact_name', 'contact_no')


class BuyOrderSerializer(serializers.ModelSerializer):
    supplier_detail = SupplierSerializer(read_only=True, source='supplier')

    class Meta:
        model = BuyOrderModel
        fields = ('id', 'supplier', 'supplier_detail', 'buy_date_time', 'digest', 'amount', 'price', 'unit', 'is_payed',
                  'is_recv_invoice', 'is_take_delivery')
        extra_kwargs = {
            'supplier': {'write_only': True}
        }


class DeliverySerializer(serializers.ModelSerializer):
    buy_order_detail = BuyOrderSerializer(read_only=True, source='buy_order')

    class Meta:
        model = TakeDeliveryModel
        fields = ('id', 'buy_order', 'buy_order_detail', 'take_delivery_date_time', 'take_delivery_no')
        extra_kwargs = {
            'buy_order': {'write_only': True}
        }


class InvoiceSerializer(serializers.ModelSerializer):
    buy_order_detail = BuyOrderSerializer(read_only=True, source='buy_order')

    class Meta:
        model = RecvInvoiceModel
        fields = ('id', 'buy_order', 'buy_order_detail', 'recv_invoice_date_time', 'recv_invoice_no',
                  'recv_invoice_file_url')
        extra_kwargs = {
            'buy_order': {'write_only': True}
        }


class PaymentSerializer(serializers.ModelSerializer):
    buy_order_detail = BuyOrderSerializer(read_only=True, source='buy_order')

    class Meta:
        model = Payment4BuyOrderModel
        fields = ('id', 'buy_order', 'buy_order_detail', 'pay_date_time', 'pay_type', 'amount', 'pay_flow_seq')
        extra_kwargs = {
            'buy_order': {'write_only': True}
        }

